package com.chezai.client.server;

import com.jx.driving.protocol.terminal.channel.Channel;

import io.netty.util.AttributeKey;

/**
 * netty通道实现
 * 
 * @author shenliuyang
 *
 *         2016年8月11日下午1:31:16
 */
public class NettyChannel implements Channel {

    private io.netty.channel.Channel c;

    public NettyChannel(io.netty.channel.Channel c) {
        super();
        this.c = c;
    }

    @Override
    public void tell(Object msg) {
        c.writeAndFlush(msg);
        c.flush();
    }

    @Override
    public void tellAndSync(Object msg) {
        try {
            c.writeAndFlush(msg).await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public <T> void attr(String key, T object) {
        c.attr(AttributeKey.valueOf(key)).set(object);
    }

    @Override
    public <T> T attr(String key) {
        AttributeKey<T> k = AttributeKey.valueOf(key);
        return c.attr(k).get();
    }

    @Override
    public void close() {
        c.close();
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> T orginal() {
        return (T) c;
    }

}
